package org.example.demo3.common;

import org.springframework.util.DigestUtils;

import java.util.UUID;

public class Salt_Encryption {
    public static String encryption(String password){
        String salt= UUID.randomUUID().toString().replace("-","");
        String salt_password= DigestUtils.md5DigestAsHex((password+salt).getBytes());
        String final_password=salt+"$"+salt_password;
        return final_password;
    }
    public static String decode(String password,String salt){

        String salt_password=DigestUtils.md5DigestAsHex((password+salt).getBytes());
        String final_password=salt+"$"+salt_password;
        return final_password;
    }
    public static boolean verify (String input_password,String final_password){
        String salt=final_password.split("\\$")[0];
        String input_password1=decode(input_password,salt);
        if (input_password1!=null&&input_password1.length()==65){
            if (final_password.equals(input_password1)){
                return true;
            }
            return false;
        }
        return false;
    }

}
